#include<stdio.h>
#include <sys/types.h>
#include<unistd.h>
#include<sys/stat.h>
#include <fcntl.h>
#include<string.h>
#include<errno.h>


int main()
{
    //读写方式打开方便我们将写入数据后打印出来
    int ret=open("./text.txt",O_RDWR);
    if(ret==-1)
    {
        printf("open fail! %s\n",strerror(errno));
        return 1;
    }
    //打开成功：
    char buff[]={"jinnzhiqi yuejianhua"};

    int n=write(ret,buff,sizeof(buff));
    if(n==-1)
    {
        printf("write fail! %s\n",strerror(errno));
        return 2;
    }
    printf("写入数据成功！\n"); 
    lseek(ret,0,SEEK_SET);
    char buff1[1024];
    int sz=read(ret,buff1,sizeof(buff1)-1);
    buff1[sz]='\0';
    printf("%s\n",buff1);
    return 0;
}
/* int main()
{
    int ret=open("./text.txt",O_RDONLY);
    if(ret==-1)
    {
        printf("open fail! %s\n",strerror(errno));
        return 1;
    }
    //打开成功：
    char buff[1024];
    int n=read(ret,buff,sizeof(buff)-1);
    if(n==-1)
    {
        printf("read fail! %s\n",strerror(errno));
        return 2;
    }
    //读取成功：
    buff[n]='\0';
    printf("%s\n",buff);


    return 0;
} */


/* int main()
{
    umask(0);
    int ret=open("./text.txt",O_WRONLY|O_CREAT,0666);
    if(ret==-1)
    {
        perror("open fail!\n");
        printf("%s\n",strerror(errno));
        return errno;
    }
    printf("文件描述符为%d\n",ret);
    return 0;
} */
